import {
  getCompanyList,
  getGroupList,
  getRoleList,
  // addCompany,
  // addGroup,
  // addRole,
  getDictChild
} from '@/api/common'

const getDefaultState = () => {
  return {
    companyList: [],
    groupList: [],
    roleList: [],
    dicMap: {}, // 字典
    dicCodes: {
      work_order_type: 'work_order_type', //  工单类型
      work_order_status: 'work_order_status', // 工单状态
      work_order_module: 'work_order_module' // 所属模块
    }
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState())
  },
  SET_LISTS: (state, { res, dataName }) => {
    const { retData } = res || {}
    const { records } = retData || {}
    state[dataName] = records || []
  }
}

const actions = {
  // 字典查询
  async getDictChild({ state }, dictCode) {
    const { dicMap } = state
    const dicItemList = dicMap[dictCode]
    if (!(dicItemList && dicItemList.length)) {
      const { retData } = await getDictChild(dictCode)
      state.dicMap = Object.assign({}, state.dicMap, {
        [dictCode]: retData || []
      })
    }
  },
  // 角色
  async getRoleList({ commit }) {
    const res = await getRoleList()
    commit('SET_LISTS', { res, dataName: 'roleList' })
  },
  // 公司
  async getCompanyList({ commit }) {
    const res = await getCompanyList()
    commit('SET_LISTS', { res, dataName: 'companyList' })
  },
  // 集团
  async getGroupList({ commit }) {
    const res = await getGroupList()
    commit('SET_LISTS', { res, dataName: 'groupList' })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
